//
//  FullScreenImageControllerViewController.m
//  BDDynamicGridViewDemo
//
//  Created by jji on 7/12/12.
//  Copyright (c) 2012 Bluedot. All rights reserved.
//

#import "FullScreenImageControllerViewController.h"
#import "ImageMaskView.h"
#import "GameControllerViewController.h"

@interface FullScreenImageControllerViewController ()

@end

@implementation FullScreenImageControllerViewController
@synthesize showImage,showImageMask;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        showScore = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 200, 20)];
        showScore.backgroundColor = [UIColor clearColor];
        showScore.textColor = [UIColor whiteColor];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.showImage removeFromSuperview];
    [self.showImageMask removeFromSuperview];
    
    [super viewWillDisappear:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    self.view = [[UIView alloc]initWithFrame:CGRectMake(0, 44, 320, 460)];
    self.view.backgroundColor = [UIColor blackColor];    
    
    self.showImageMask.backgroundColor = [UIColor greenColor];    
    if(showImage.image.size.width >=320 || showImage.image.size.height >=460)
        
    {
        showImage.frame = CGRectMake((self.view.frame.size.width-320)/2, (self.view.frame.size.height-460)/2, 320,460);
        self.showImageMask.frame = showImage.frame;
    }
    else {
        showImage.frame = CGRectMake((self.view.frame.size.width-showImage.image.size.width)/2, (self.view.frame.size.height-showImage.image.size.height)/2, showImage.image.size.width,showImage.image.size.height);
        
    }
    showImage.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:showImage];
    
    //    
    //    CGRect rect = CGRectMake(0, 0, 1, 1);
    //    UIGraphicsBeginImageContext(rect.size);
    //    CGContextRef context = UIGraphicsGetCurrentContext();
    //    UIGraphicsPushContext(context);
    //    
    //    CGContextSetFillColorWithColor(context,
    //                                   [[UIColor greenColor] CGColor]);
    //    CGContextFillRect(context, rect);
    //    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
    
    UIImage*img = [UIImage imageNamed:@"placeholder.png"];
    
    self.showImageMask = [[ImageMaskView alloc] initWithFrame:showImage.frame image:img];
    
    showImageMask.userInteractionEnabled = YES;
    
    self.showImageMask.imageMaskFilledDelegate = self;
    [self.view addSubview:showImageMask];
    
    [self.view addSubview:showScore];
    showScore.text = [NSString stringWithFormat:@"剩余墨水： %d", [[GameControllerViewController sharedGameControllerInstance] GetUserGold]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)imageMaskView:(ImageMaskView *)maskView cleatPercentWasChanged:(float)clearPercent {
	//NSLog(@"percent: %.2f", clearPercent);
    int usergold = [[GameControllerViewController sharedGameControllerInstance] GetUserGold];
    if (usergold > 1)
    {
        int usedg = clearPercent;
        usergold -= usedg;
        [[GameControllerViewController sharedGameControllerInstance] SetUserGold:usergold];
        showScore.text = [NSString stringWithFormat:@"剩余墨水： %d", usergold];
    }
    else
    {
        showImageMask.userInteractionEnabled = NO;
    }
}

- (void)imageMaskView:(ImageMaskView *)maskView finished:(float)clearPercent
{
    [self.showImageMask removeFromSuperview];
    [[GameControllerViewController sharedGameControllerInstance] SetUserPassedLevel:showImage.tag];
    
    UIAlertView * al = [[UIAlertView alloc] initWithTitle:nil message:@"恭喜您解锁此美女了！" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [al show];
}

@end
